package com.yixin.hospital.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.AuditFlowPreset;
import com.yixin.hospital.entity.domain.JobDuty;
import com.yixin.hospital.entity.domain.User;
import com.yixin.hospital.entity.dto.req.auditflowpreset.AuditFlowPresetAddReqDTO;
import com.yixin.hospital.entity.dto.req.auditflowpreset.AuditFlowPresetEditReqDTO;
import com.yixin.hospital.entity.vo.AuditFlowPresetVO;
import com.yixin.hospital.entity.vo.JobDutyVO;
import com.yixin.hospital.enums.BusinessEnum;
import com.yixin.hospital.service.AuditFlowPresetService;
import com.yixin.hospital.service.DeptService;
import com.yixin.hospital.service.JobDutyService;
import com.yixin.hospital.service.UserService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/audit-flow-preset")
@Api(value = "审批流管理", tags = "审批流管理")
public class AuditFlowPresetController {

    @Resource
    private AuditFlowPresetService auditFlowPresetService;

    @Resource
    private JobDutyService jobDutyService;

    @Resource
    private UserService userService;
    @Resource
    private DeptService deptService;

    @GetMapping("/list")
    @ApiOperation(value = "审批流列表")
    public ApiResult<?> list() {
        List<AuditFlowPreset> list = auditFlowPresetService.list();
        List<AuditFlowPresetVO> resp = new ArrayList<>();
        for (AuditFlowPreset auditFlowPreset : list) {
            AuditFlowPresetVO vo = new AuditFlowPresetVO();
            BeanUtil.copyProperties(auditFlowPreset, vo, "jobDuty");
            vo.setBusinessName(BusinessEnum.getDesc(auditFlowPreset.getBusinessCode()));
            List<JobDutyVO> jobDutyList = new ArrayList<>();
            String[] split = auditFlowPreset.getJobDuty().split(",");
            for (String jobDutyId : split) {
                JobDuty jobDuty = jobDutyService.getById(Long.valueOf(jobDutyId));
                JobDutyVO jobDutyVO = new JobDutyVO();
                BeanUtils.copyProperties(jobDuty, jobDutyVO);
                jobDutyVO.setDeptName(deptService.getById(jobDuty.getDeptId()).getName());
                jobDutyList.add(jobDutyVO);
            }
            vo.setJobDutyList(jobDutyList);
            resp.add(vo);
        }
        return ApiResult.success(resp);
    }

    @GetMapping("/info")
    @ApiOperation(value = "审批流详情（带人员）")
    public ApiResult<?> info(@RequestParam(value = "businessCode") String businessCode) {
        LambdaUpdateWrapper<AuditFlowPreset> wrapper = new LambdaUpdateWrapper<>();
        if (StringUtils.isNotBlank(businessCode)) wrapper.eq(AuditFlowPreset::getBusinessCode, businessCode);
        AuditFlowPreset auditFlowPreset = auditFlowPresetService.getOne(wrapper);
        List<JobDutyVO> jobDutyList = new ArrayList<>();
        String[] split = auditFlowPreset.getJobDuty().split(",");
        for (String jobDutyId : split) {
            JobDutyVO jobDutyVO = new JobDutyVO();
            JobDuty jobDuty = jobDutyService.getById(Long.valueOf(jobDutyId));
            BeanUtils.copyProperties(jobDuty, jobDutyVO);
            List<User> userListByJobDuty = userService.list(new LambdaQueryWrapper<User>().like(User::getJobDuty, jobDuty.getId()));
            userService.list().stream().filter(user -> {
                String[] jobDutyListByUser = user.getJobDuty().split(",");
                for (String s : jobDutyListByUser) {
                    if (Objects.equals(s, jobDuty.getId())) return true;
                }
                return false;
            });
            jobDutyVO.setAuditAvailableUserList(userListByJobDuty);
            jobDutyList.add(jobDutyVO);
        }
        return ApiResult.success(jobDutyList);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加审批流")
    public ApiResult add(@Valid @RequestBody AuditFlowPresetAddReqDTO auditFlowPresetAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        AuditFlowPreset auditFlowPreset = new AuditFlowPreset();
        auditFlowPreset.setBusinessCode(auditFlowPresetAddReqDTO.getBusinessCode());
        auditFlowPreset.setJobDuty(auditFlowPresetAddReqDTO.getJobDutyList());
        auditFlowPreset.setCreatedBy(userName);
        auditFlowPreset.setUpdatedBy(userName);
        auditFlowPresetService.save(auditFlowPreset);
        return ApiResult.success();
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑审批流")
    public ApiResult edit(@Valid @RequestBody AuditFlowPresetEditReqDTO auditFlowPresetEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        AuditFlowPreset auditFlowPreset = new AuditFlowPreset();
        auditFlowPreset.setId(auditFlowPresetEditReqDTO.getId());
        auditFlowPreset.setJobDuty(auditFlowPresetEditReqDTO.getJobDutyList());
        auditFlowPreset.setUpdatedBy(userName);
        auditFlowPresetService.updateById(auditFlowPreset);
        return ApiResult.success();
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除审批流")
    public ApiResult remove(@PathVariable int id) {
        auditFlowPresetService.removeById(id);
        return ApiResult.success();
    }
}
